-- damage_random_mon
-- create by wangy
-- 技能：随机攻击多个敌人，根据攻击（角斗场特殊场景）
-- 目前支持如下配置：
-- 1、damage_random_mon(1200,3)：对3个目标造成1200威力的魔法伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 2. 攻击方的攻击
        local force = para[1];
        local attack = source:getAttack();

        -- 3. 伤害，先对玩家进行攻击，必定被攻击到
        local damage = math.modf(force * attack / 1000);
        CombatM.receiveDamage(source, target, damage, skillId, extra);

        -- 4. 遍历所有的怪
        local ret = {};
        local monster;
        for p = 1, GRID_SIZE do
            local grid = DungeonM.getGridByPos(p);
            monster = grid.monster;

            repeat

            -- 是未死亡的怪物，排除自身
            if  not grid:isOpened() or
                not monster or
                grid.monster:isDead() or
                monster == source then
                break;
            end

            table.insert(ret, monster);

            until true;
        end

        if #ret == 0 then
            return 0;
        end

        -- 去除玩家数量1
        local count = para[2] - 1;
        for index = 1, count do
            local num = #ret;
            -- 随机选一个进行打击
            local rand = DungeonM.getRandSeed("damage_random_mon") % num;
            local monster = ret[rand + 1];

            -- 增加命中动作
            if SkillM.getSequence(monster) == nil then
                SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            end
            SkillM.getSequence(monster):hit(source, monster, skillId);
            -- 受创
            CombatM.receiveDamage(source, monster, damage, skillId, extra);

            -- 如果目标已经死了，从待打击列表中移除
            if monster:isDead() then
                table.removeItem(ret, monster);
                -- 如果全死了
                if #ret == 0 then
                    break;
                end
            end
        end

        -- 作用目标
        return damage;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        if (#arr == 1) then
            return { tonumber(arr[1]), };
        else
            return { tonumber(arr[1]), tonumber(arr[2]) };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
};
